#include "stdafx.h"
#include "Environment.h"
#include "../CmdFwk/InteractiveCmd.h"
#include "../CmdFwk/CmdMgr.h"

using namespace Base::Env;

Environment::Environment(const void* id,
                         const std::wstring& title,
                         const void* defaultCmdId)
    : m_state(eNormal)
    , m_id(id)
    , m_title(title)
    , m_cmdId(0)
    , m_defaultCmdId(defaultCmdId)
{
}

Environment::~Environment()
{    
}

const std::wstring&
Environment::title() const
{
    return m_title;
}

const void*
Environment::cmd() const
{
    return m_cmdId;
}

void
Environment::cmd(const void* cmdId)
{
    if (m_cmdId != cmdId)
        m_cmdId = cmdId;
}

const void*
Environment::defaultCmd() const
{
    return m_defaultCmdId;
}

const void*
Environment::id() const
{
    return m_id;
}

Environment::EState
Environment::state() const
{
    return m_state;
}

void
Environment::state(EState state)
{
    if (m_state != state)
    {
        if (   eNormal == m_state
            && eActive ==   state)
        {
            using namespace Base::CmdFwk;

            if (m_cmdId)
                CmdMgr::get()->doCmd(m_cmdId);
            else if (m_defaultCmdId)
                CmdMgr::get()->doCmd(m_defaultCmdId);

            onActivate();
        }
        else if (   eActive == m_state
                 && eNormal ==   state)
        {
            using namespace Base::CmdFwk;            

            if (InteractiveCmd* activeCmd = CmdMgr::get()->activeCmd())
            {
                if (   m_cmdId
                    && activeCmd->id() == m_cmdId)
                {
                    CmdMgr::get()->deactivateCmd();
                }
            }

            onDeactivate();
        }
        else
        {
            assert(false);
        }

        m_state = state;
    }
}

void
Environment::onActivate()
{
}

void
Environment::onDeactivate()
{
}